// Group.cs
// Facebook/Framework/Service
// Copyright (c) 2007, Nikhil Kothari. All Rights Reserved.
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using Facebook.Service.Core;

namespace Facebook.Service {

    /// <summary>
    /// Represents a Facebook group.
    /// </summary>
    public sealed class Group : FacebookEntity {

        private bool _idTransformed;
        private bool _creatorTransformed;

        internal Group(Hashtable groupState)
            : base(groupState) {
        }

        /// <summary>
        /// The unique user ID of the group's creator if available.
        /// </summary>
        public string CreatorUserID {
            get {
                if (_creatorTransformed == false) {
                    return TransformCreator();
                }
                return (string)this["creator"];
            }
        }

        /// <summary>
        /// The description of the group.
        /// </summary>
        public string Description {
            get {
                return GetState<string>("description") ?? String.Empty;
            }
        }

        /// <summary>
        /// The unique ID of the group.
        /// </summary>
        public string ID {
            get {
                if (_idTransformed == false) {
                    return TransformID();
                }
                return (string)this["gid"];
            }
        }

        /// <summary>
        /// The URL of the larger version of the group's profile picture.
        /// </summary>
        public string LargePictureUrl {
            get {
                return GetState<string>("pic_big") ?? String.Empty;
            }
        }

        /// <summary>
        /// The name of the group.
        /// </summary>
        public string Name {
            get {
                return GetState<string>("name") ?? String.Empty;
            }
        }

        /// <summary>
        /// The ID of the network that the group belongs to, and 0 if the group is
        /// a global group.
        /// </summary>
        public string NetworkID {
            get {
                return null;
            }
        }

        /// <summary>
        /// The URL of the the group's profile picture.
        /// </summary>
        public string PictureUrl {
            get {
                return GetState<string>("pic") ?? String.Empty;
            }
        }

        /// <summary>
        /// The URL of the small version of the group's profile picture.
        /// </summary>
        public string SmallPictureUrl {
            get {
                return GetState<string>("pic_small") ?? String.Empty;
            }
        }

        /// <summary>
        /// The sub-type of the group.
        /// </summary>
        public string SubType {
            get {
                return GetState<string>("group_subtype") ?? String.Empty;
            }
        }

        /// <summary>
        /// The type of the group.
        /// </summary>
        public string Type {
            get {
                return GetState<string>("group_type") ?? String.Empty;
            }
        }

        private string TransformCreator() {
            Debug.Assert(_creatorTransformed == false);

            string id = FacebookResponse.CoerceString(this["creator"]);

            this["creator"] = id;
            _creatorTransformed = true;

            return id;
        }

        private string TransformID() {
            Debug.Assert(_idTransformed == false);

            string id = FacebookResponse.CoerceString(this["gid"]);

            this["gid"] = id;
            _idTransformed = true;

            return id;
        }
    }
}
